package me.shiqiang.im.common;

import com.corundumstudio.socketio.SocketIOClient;

import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by maliangxue on 2018/4/15.
 */
public class ConnectionServer {
    public String serverId;
    public ConcurrentHashMap<String,SocketIOClient> businessProcessors = new ConcurrentHashMap<>(8);

    public ConnectionServer(String serverId){
        this.serverId = serverId;
    }

    public void removeProcessor(SocketIOClient processor) {
        businessProcessors.remove(processor.getSessionId().toString());
    }

    public void addProcessor(SocketIOClient processor) {
        businessProcessors.put(processor.get("machineId"), processor);
    }

    public SocketIOClient nextProcessor(){
        int size = businessProcessors.size();
        Random random = new Random();
        int index = random.nextInt(size);
        ArrayList<SocketIOClient> list = new ArrayList<>();
        list.addAll(businessProcessors.values());
        return list.get(index);
    }
}
